<template>
  <Layout>

    <Form ref="form1" :model="form1" :label-width="120">
      <Row class="query-form" style="padding-top: 15px">
        <Col span="8">
          <FormItem label="审计任务：">
            <Input class="query-form-el" type="text" v-model="form1.missionName"/>
          </FormItem>
        </Col>
        <Col span="16">
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
    </Form>

    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据！" :loading="buttonLoading" size="small"
           border :columns="columns1" :data="dataTable">
    </Table>

    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
  </layout>
</template>

<script>
import {Api} from '@/api/index.js'
import {sessionStorage} from '@/common/cacheUtils.js';

export default {
  components: {},
  data() {
    return {
      createUserId: '',
      form1: {},
      form2: {
        members: '',
        membersId: ''
      },
      //分页参数
      pageSizeArray: [10, 20, 50],
      pagedata: {
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },

      columns1: [
        {
          title: "序号",
          width: 80,
          align: "center",
          fixed: 'left',
          render: (h, params) => {
            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
          }
        },
        {
          minWidth: 230,
          title: '审计任务',
          key: 'missionName',
          align: 'center',
        },
        {
          minWidth: 250,
          title: '审计部门',
          key: 'auditedOrgName',
          align: 'center'
        },
        {
          title: '带队',
          minWidth: 150,
          align: 'center',
          key: 'supervisor',
        },
        {
          minWidth: 100,
          title: '组长',
          key: 'groupLeaderName',
          align: 'center',
        },
        {
          minWidth: 200,
          title: '成员',
          key: 'membersName',
          align: 'center',
        },


        // {
        //     width: 150,
        //     title: '审计类型',
        //     key: 'auditTypeName',
        //     align: 'center'
        // },
        {
          minWidth: 150,
          title: '开始时间',
          key: 'planStart',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              (this.formatDate(new Date(params.row.planStart), 'yyyy-MM-dd'))
            ]);
          }

        },
        {
          minWidth: 150,
          title: '结束时间',
          key: 'planEnd',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              (this.formatDate(new Date(params.row.planEnd), 'yyyy-MM-dd'))
            ]);
          }
        },

        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          minWidth: 100,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('A', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  fontSize: '12px',
                },
                on: {
                  click: () => {
                    this.save(params.row);
                  }
                }
              }, '选择'),
            ]);
          }
        }
      ],
      buttonLoading: false, tableHeight: 450,
      dataTable: [],
    }
  },
  props: ['auditState', 'filterState'],
  mounted: function () {
    this.tableHeight = window.innerHeight - 420;
    this.createUserId = JSON.parse(sessionStorage('paramUserInfo').get('userInfo'))["createUserId"]
    this.query();
  },
  methods: {
    pageChange(pageNum) {
      this.pagedata.currentPage = pageNum;
      this.query();
    },
    pageSizeChange(pageSize) {
      this.pagedata.pageSize = pageSize;
      this.query();
    },

    copyArray(arr) {
      return JSON.parse(JSON.stringify(arr));
    },


    save(row) {
      this.$emit('selectMission', row);

    },

    closeModal() {
      this.$emit('closeModal1');
    },

    query() {
      let userId = sessionStorage('userId').getObject('userId').userId;
      let orgId = sessionStorage('userId').getObject('userId').companyId;
      let role = "";
      let userRole = sessionStorage('userId').getObject('userId').userRole
      userRole.forEach(item => {
        role += item.roleId + ",";
      })
      let data = {
        currentPage: this.pagedata.currentPage,
        size: this.pagedata.pageSize,
        createOrgId: orgId,
        createUserId: userId,
        auditState:'1',
        role: role
      };
      data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));
      data.groupLeaderName = this.form1.groupLeaderName || ''
      let url = Api.queryAdtMission;
      this.$http.get(url, {params: data}).then((res) => {
        this.curPlan = res.data.plan
        this.dataTable = res.data.data;
        this.pagedata.total = res.data.totalSize;
        this.buttonLoading = false
      }).catch(e => {
        console.error(e)
        this.$Message.error("数据获取失败")
      })
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
        }
      }
      return fmt;
    },
    padLeftZero(str) {
      return ('00' + str).substr(str.length);
    },
    reset() {
      this.form1 = {}
    },
  }

}
</script>
